package com.ruoyi.tester.executor.expression;

import com.ruoyi.tester.executor.ExpressionContext;
import org.json.JSONArray;
import org.springframework.stereotype.Component;

/**
 * @author SongJun
 * @description 解析求长度函数size()
 * @date 2022/4/21 15:08
 */
@Component
public class ExpSize implements ExpressionAnalysis {
    @Override
    public String getExpressionName() {
        return "size";
    }

    @Override
    public String getDescription() {
        return "size() 返回当前数组类型上下文所包含的元素数量;";
    }

    @Override
    public Object analysis(ExpressionContext context, String... param) {
        JSONArray node;
        if(context.getContext() instanceof JSONArray){
            node = (JSONArray) context.getContext();
        } else if(context.getContext() instanceof String){
            node = new JSONArray((String)context.getContext());
        } else {
            node = new JSONArray(context.getContext());
        }
        return node.length();
    }
}
